package com.gift.shiro;


import com.gift.common.MD5Utility;
import com.gift.dao.UserTableMapper;
import com.gift.pojo.UserTable;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;

public class AuthRealm extends AuthorizingRealm {

    @Resource
    UserTableMapper userTableMapper;



    // 认证登录
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        String username = usernamePasswordToken.getUsername();
         int result = userTableMapper.checkName(username);
        if (result != 1) {
            throw new UnknownAccountException();
        }
        char[] pW = usernamePasswordToken.getPassword();
        String passWord = String.valueOf(pW);
        UserTable user = userTableMapper.login(username,passWord);
        if(user == null){
            throw new IncorrectCredentialsException();
        }
        if(user.getIsuse()==0){
            throw new DisabledAccountException();
        }
        return new SimpleAuthenticationInfo(user, user.getPassword(), this.getClass().getName());
    }


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }
}
